import { Component, OnInit, Input, ViewChild, EventEmitter, Output } from '@angular/core';
import { Subject, BehaviorSubject, Observable } from 'rxjs';
import { distinctUntilChanged, debounceTime, switchMap } from 'rxjs/operators';
import { NzInputModule } from 'ng-zorro-antd';
import { HttpService } from 'lbf';

@Component({
  selector: 'lb-grsy',
  templateUrl: './lb_grsy.html'
})

export class LbGrsyComponent implements OnInit {

  searchChange$ = new BehaviorSubject('');
  isLoading = false;
  dataAAC001 = '';
  options: any[] = [];
  

  @Output() changed =new EventEmitter<string>();

  @Input() set value(val: any) {

    this.dataAAC001 = val;

  }

  get value(): any {
    return this.dataAAC001;
  }

  nzFilterOption = () => true;

  constructor(private lbs: HttpService) { }

  reset(value) {
    this.dataAAC001 = value;
  }

  change(value:string):void{
    this.changed.emit(value);
  }

  setvalue(value) {
    if (!value)
      this.dataAAC001 = null;

    this.dataAAC001 = value;
  }

  onSearch(value: string): void {
    this.isLoading = true;
    this.searchChange$.next(value);
  }

  search(serachParam: string) {
    console.log(serachParam);
    // this.searchTerms.next(serachParam);
    this.queryData(serachParam);
    /*
   if ( event.key === "Enter" ) 
     {
        console.log(serachParam);
        this.queryData(serachParam);
        
     }
     else if(event.key === "ArrowDown" || event.key ==="ArrowUp")
     {
         // console.log(this.grsy);
         console.log('值：'+this.dataAAC001);
     }
     else
     {
        console.log(event.key);
       this.searchTerms.next(serachParam);
     }*/
  }


  onKeyDown(event: KeyboardEvent) {
    // tslint:disable-next-line: prefer-conditional-expression
    if (event.key === "Enter") {

      this.queryData(this.dataAAC001);

    }
    else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
      // console.log(this.grsy);
      console.log('值：' + this.dataAAC001);
    }
    else {
      // console.log(event.key);
      // this.options = [];
    }

  }

  queryData(val: string) {
    console.log('查询==》' + val);
    this.lbs.lbservice('grsy_data', { para: val, page: { 'PAGESIZE': 10, 'PAGEIDX': 1 } }, 500).then(resdata => {
      this.isLoading = false;
      if (resdata.code < 1) {
        // this.msgSrv.error(resdata.errmsg);
        console.log('查询结果1==》' + resdata);
        this.options = [];
      }
      else {
        if (resdata.page.COUNT === 0) {
          this.options = [];
        }
        else if (resdata.page.COUNT === 1) {
          // 直接赋值
        }
        else {
          this.options = resdata.message.list;
          console.log('----');
          // this.isOptionVisible = true;
        }


      }

    });
    // this.options = [{AAC001:'00230122',TEXT:"00230122 - 张三丰 - 330124198201230314"},{AAC001:'00230123',TEXT:"00230123 | 李四友 | 330124197201230322"},{AAC001:'00230123',TEXT:"00230123 | 李四友 | 330124197201230322"}];

  }


  ngOnInit(): void {



    const optionList$ = this.searchChange$
      .asObservable()
      .pipe(debounceTime(500));
    optionList$.subscribe(data => {
      this.search(data);
    });

  }


}